import time
from libs.base.action import Action


class Wechat(Action):
    """
    封装微信主程序相关的操作
    """

    def __init__(self, driver):
        self.driver = driver

    def switch_tab(self, tab):
        """切换微信主页tab"""
        locater = "//android.widget.TextView[@text='%s']/../.." % tab
        self.find_by_xpath(locater).click()

    def switch_tab_chat(self):
        """切换到 微信聊天tab"""
        self.switch_tab("微信")

    def switch_tab_find(self):
        """切换到 发现tab"""
        self.switch_tab("发现")

    def search_miniprogram(self, name="糖豆爱生活"):
        """搜索微信小程序，并打开"""
        self.switch_tab_find()
        self.find_by_xpath("//android.widget.TextView[@text='小程序']/..").click()
        time.sleep(3)
        self.find_by_access("搜索").click()
        time.sleep(2)
        self.find_by_xpath("//android.widget.EditText[@text='搜索小程序']").click()
        self.find_by_xpath("//android.widget.EditText[@text='搜索小程序']").send_keys(name)
        time.sleep(2)
        self.keyevent(66) #搜狗输入法可用
        time.sleep(8)
        try:
            self.click_search_result_by_native(name)  # 先尝试原生控件点击
        except Exception as e:
            print("Android原生方法定位出错，尝试webview方式点击，")
            self.switch_context("WEBVIEW_com.tencent.mm:toolsmp")
            self.switch_window(self.click_search_result_by_webview, name)
            self.switch_context_to_native()
        time.sleep(8)

    def get_wechat_number(self):
        """读取微信号"""
        wx_number = None
        self.switch_tab("我")
        str = self.find_by_xpath("//android.widget.TextView[contains(@text,'微信号')]").text
        if str.contains("微信号"):
            wx_number = str.split(":")[1]
        return wx_number

    def logout_wechat(self):
        """注销微信"""
        self.switch_tab("我")
        self.find_by_xpath("//android.widget.TextView[@text='设置']/..").click()
        time.sleep(2)
        self.swip_by_coordinates([400, 1000], [400, 500])
        self.find_by_xpath("//android.widget.TextView[@text='退出']/../..").click()
        time.sleep(1)
        self.find_by_xpath("//android.widget.TextView[@text='退出登录']/..").click()
        self.find_by_xpath("//android.widget.Button[@text='退出']").click()
        self.wait_object(self.find_by_xpath("//android.widget.Button[@text='更多']"), 5)

    def login_by_wechat_number(self, username, password):
        """用微信号/QQ/邮箱 执行登录操作"""
        if self.is_exist("//android.widget.TextView[@text='发现']"):  # 如果已经登录，则跳出
            return
        self.find_by_xpath("//android.widget.Button[@text='更多']").click()
        self.find_by_xpath("//android.widget.TextView[@text='登录其他帐号']/../..").click()
        self.find_by_xpath("//android.widget.Button[@text='用微信号/QQ号/邮箱登录']").click()
        self.find_by_xpath("//android.widget.EditText[@text='请填写微信号/QQ号/邮箱']").send_keys(username)
        self.find_by_xpath("//android.widget.TextView[@text='密码']/following-sibling::android.widget.EditText[1]").click()
        self.find_by_xpath("//android.widget.TextView[@text='密码']/following-sibling::android.widget.EditText[1]").send_keys(password)
        self.find_by_xpath("//android.widget.Button[@text='登录']").click()
        time.sleep(8)

    def open_miniprogram_from_index(self, name):
        """从微信首页下拉，打开小程序"""
        self.switch_tab_chat()
        self.swip_by_coordinates()
        locater = "//android.widget.TextView[contains(@text,'%s')]" %name
        self.find_by_xpath(locater).click()
        time.sleep(5)

    def choose_share_to(self, shareto, message=None):
        """调起微信分享到微信后，选择要分享的好友对话，并确认发送"""
        locater = "//android.widget.TextView[contains(@text,'%s')]/../../../.." % shareto
        self.find_by_xpath(locater).click()  # 选择分享的对话框
        if message:
            self.find_by_xpath("//android.widget.EditText[@text='给朋友留言']").click()
            self.find_by_xpath("//android.widget.EditText[@text='给朋友留言']").send_keys(message)
            self.hide_keyboard()  # 隐藏键盘
        self.find_by_xpath("//android.widget.Button[@text='发送']").click()  # 确认分享

    def close_miniprogram(self):
        """点击右上角圆点 关闭小程序"""
        self.tap_by_scale([0.9, 0.05])

    def click_foward_btn(self):
        """点击。。。弹出的转发按钮"""
        self.find_by_xpath("//android.widget.TextView[@text='转发']/../..").click()

    # ===================================================================
    # webview相关操作

    def click_search_result_by_native(self, name):
        """安卓原生方法：点击搜索出的第一个小程序"""
        locater = "//android.view.View[@content-desc='%s']" %name
        self.find_by_xpath(locater).click()

    def click_search_result_by_webview(self, name):
        """webview 方法：点击搜索出的第一个小程序"""
        locater = "//em[text()='%s']/ancestor::div[@class='search_item_inner']" %name
        self.find_by_xpath(locater).click()